<?php
namespace app\index\model;
use think\Db;
use think\Model;

class SubTestpaper extends Model
{
	public function createPaper($param){
		$user = user();

		self::insert([
			'user_id' => $user['id'],
			'ques_id' => implode(',', $param['quesIds']),
			'meta_id' => $param['meta_id'],
			'meta_name' => $param['meta_name'],
			'course_id' => $param['course_id'],
			'start_time' => time(),
			'updated_time' => time(),
			'end_time' => time(),
		]);

		return self::getLastInsID();
	}

	public function getPaperQues($id){
		$data = self::getOneData(['id'=>$id]);
		$usedTime = time()-$data['start_time'];
		$qCount = SubUser::where('t_id',$id)->where('status',1)->count();
		$status = 0;
		if($qCount == count(explode(',',$data['ques_id']))) $status = 1;
		self::where('id',$id)->update(['updated_time'=>time(),'end_time'=>time(),'used_time'=>$usedTime,'status'=>$status]);
		return $data;
	}

	public function getOneData($param, $field = ''){
		return self::when($param['id'], function($query) use ($param){
							$query->where('id', $param['id']);
						})
						->when($param['course_id'], function($query) use ($param){
							$query->where('course_id', $param['course_id']);
						})
						->when($field, function($query) use ($param, $field){
							$query->where('meta_name', $field)
								->where('meta_id', $param[$field]);
						})
						->where('status', 0)
						->find();
	}
}